package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;

import com.ym.medical.domain.entity.LabelsEntity;
import com.ym.medical.domain.param.LabelsParam;
import com.ym.medical.domain.vo.LabelsVo;
import com.ym.medical.service.ILabelsSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 医生相关标签库管理
 *
 * @author: 李涛
 * @version: 2019年06月19日 17:29
 */
@Api(tags = "医生相关标签库管理")
@Controller
@RequestMapping("/management/doctor/labels")
@Validated
public class DoctorLabelsController extends BaseController {

	@Autowired
	private ILabelsSV labelsSV;

	@ApiOperation("标签列表查询")
	@GetMapping("/list")
	@ResponseBody
	public APIResponse<PageInfo<LabelsEntity>> tagListQuery(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			LabelsParam labelsModel) throws Exception {
		PageHelper.startPage(pageNo,limit,"id desc");
		List<LabelsEntity> labelsPageInfo = labelsSV
				.selectList(labelsModel.covert());
		return successPage(labelsPageInfo);
	}

	@Log
	@ApiOperation("标签新增或修改")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public APIResponse<Long> tagUpdate(
			@RequestBody LabelsEntity labels) {
		Long currId = ShiroUtil.getLoginId();
		if (labels.getId() != null) {
			labelsSV.updateById(labels);
		} else {
			labels.setCreateSb(currId);
			if (labels.getParentId() == null) {
				labels.setParentId(-1L);
			}
			labelsSV.save(labels);
		}
		return successData(labels.getId());
	}

	@ApiOperation("标签删除")
	@PostMapping(value = "/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public APIResponse tagDelete(
			@NotNull @RequestBody Long[] ids
	) {
		labelsSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("标签列表查询")
	@GetMapping("/dept/list")
	@ResponseBody
	public APIResponse<PageInfo<LabelsVo>> deptList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "deptName", value = "科室名称", required = false)
			@RequestParam(name = "deptName", required = false)
					String deptName,
			@ApiParam(name = "diseaseName", value = "疾病名称", required = false)
			@RequestParam(name = "diseaseName", required = false)
					String diseaseName
	) throws Exception {
		PageInfo<LabelsVo> labelsPageInfo = labelsSV
				.listPageCustomer(deptName, diseaseName, pageNo, limit);
		return APIResponse.success(labelsPageInfo);
	}

}
